package edu.ucmo.mathcs.afc39250;

//TODO: Add comments.

import java.io.StringWriter;
import java.util.ArrayList;

import org.lsmp.djep.djep.*;
import org.lsmp.djep.xjep.*;
import org.nfunk.jep.*;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CalculatorActivity extends Activity implements OnKeyListener, OnKeyboardActionListener{
    /** Called when the activity is first created. */
	JEP parser;
	DJep parser2;
	PrintVisitor printer;
	protected int count = 1;
	protected ArrayList<String> answers = new ArrayList<String>(); 
	private static final String TAG = CalculatorActivity.class.getName();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        parser = new JEP();
        parser2 = new DJep();
        parser2.addStandardConstants();
        parser2.addStandardFunctions();
        parser2.addComplex();
        parser2.setAllowUndeclared(true);
        parser2.setAllowAssignment(true);
        parser2.setImplicitMul(true);
        parser2.addStandardDiffRules();
        parser.addStandardConstants();
        parser.addStandardFunctions();
        parser.setAllowUndeclared(true);
        
        
        EditText editText = (EditText)findViewById(R.id.editText1);
        //editText.setInputType(0);
       
        KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
        Keyboard keyboard = new Keyboard(this, R.xml.keyboard);
        keysize(keyboard);
        keyboardView.setKeyboard(keyboard);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener()
        {

			public void swipeUp() {
          	  Log.d(TAG, "swipeUp");
          	 }

          	 public void swipeRight() {
          	  Log.d(TAG, "swipeRight");
          	 }

          	 public void swipeLeft() {
          	  Log.d(TAG, "swipeLeft");
          	 }

          	 public void swipeDown() {
          	  Log.d(TAG, "swipeDown");
          	 }

          	 public void onText(CharSequence text) {
          	  Log.d(TAG, "onText? \"" + text + "\"");
          	 }

          	 public void onRelease(int primaryCode) {
          	  Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
          	 }

          	 public void onPress(int primaryCode) {
          	  Log.d(TAG, "onPress? primaryCode=" + primaryCode);
          	 }

          	 public void onKey(int primaryCode, int[] keyCodes) {
          		 if (primaryCode == -1)
          		 {
          			EditText input = (EditText)findViewById(R.id.editText1);
    				int getCursor = input.getSelectionStart();
    				StringBuilder instring = new StringBuilder(input.getText().toString());
    				if(getCursor >0)
    				{
    					instring.replace(getCursor-1, getCursor, "");
    					input.setText(instring);
    					input.setSelection(getCursor-1);
    				}
          		 }
          		 else if(primaryCode == 4)
          		 {
          			EditText input = (EditText)findViewById(R.id.editText1);
    				input.setText("");
    				 
          		 }
          		else if(primaryCode == 5)
         		 {
         			EditText input = (EditText)findViewById(R.id.editText1);
         			TextView answerField = (TextView)findViewById(R.id.solution);
         			input.setText("");
         			answerField.setText("");
         			answers.clear();
         			count = 1;
         		 }
          		 else if(primaryCode == 2)
          		 {
          			EditText input = (EditText)findViewById(R.id.editText1);
    				int getCursor = input.getSelectionStart();
    				StringBuilder instring = new StringBuilder(input.getText().toString());
    				instring.insert(getCursor, "pi");
    				input.setText(instring);
					input.setSelection(getCursor+2);
          		 }
          		else if(primaryCode == 8)
        		 {
        			EditText input = (EditText)findViewById(R.id.editText1);
        			int getCursor = input.getSelectionStart();
        			StringBuilder instring = new StringBuilder(input.getText().toString());
        			instring.insert(getCursor, "ans");
        			input.setText(instring);
					input.setSelection(getCursor+3);
        		 }
          		else if(primaryCode == 1)
         		 {
         			EditText input = (EditText)findViewById(R.id.editText1);
         			int getCursor = input.getSelectionStart();
         			StringBuilder instring = new StringBuilder(input.getText().toString());
         			instring.insert(getCursor, "e");
         			input.setText(instring);
					input.setSelection(getCursor+1);
         		 }
          		else if(primaryCode == 6)
         		 {
         			EditText input = (EditText)findViewById(R.id.editText1);
         			int getCursor = input.getSelectionStart();
         			if(getCursor>0)
         				input.setSelection(getCursor-1);
         		 }
         		else if(primaryCode == 7)
        		 {
        			EditText input = (EditText)findViewById(R.id.editText1);
        			int getCursor = input.getSelectionStart();
        			if(getCursor<input.getText().toString().length())
						input.setSelection(getCursor+1);
        		 }
          		else if(primaryCode == 3)
        		 {
          			InputMethodManager inputManager = (InputMethodManager)
          	        		getSystemService(Context.INPUT_METHOD_SERVICE); 
          	        		inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

          	        		EditText input = (EditText)findViewById(R.id.editText1);
          	        		TextView answerField = (TextView)findViewById(R.id.solution);
          	        		
          	        		
          	                
          	                if(answerField.getText().toString()=="")
          	                {
          	                	String answer = null;
								try
								{
									String in = findAns(input.getText().toString());
									Node node = parser2.parse(in);
									
									String noo = parser2.toString(parser2.simplify(parser2.simplify(parser2.preprocess(node))));
									answer = String.format("%s\n%s", "ans" + count++ + ": " + noo, input.getText().toString());
									answers.add(input.getText().toString());
								}
								catch (ParseException e)
								{
									e.printStackTrace();
									answer = answerField.getText().toString();
								}
								//answer = String.format("%s\n%s", input.getText().toString(), "");
          	                	answerField.setText(answer);
          	                }
          	                else
          	                {
          	                	String answer = null;
								try
								{	
									String in = findAns(input.getText().toString());
									Node node = parser2.parse(in);
									
									String noo = parser2.toString(parser2.simplify(parser2.simplify(parser2.preprocess(node))));
									answer = String.format("%s\n%s\n%s", "ans" + count++ + ": " + noo, input.getText().toString(), answerField.getText().toString());
									answers.add(input.getText().toString());
								}
								catch (ParseException e)
								{
									// TODO Auto-generated catch block
									e.printStackTrace();
									answer = answerField.getText().toString();
								}
								answerField.setText(answer);
          	                }
        		 }
          		 else
          		 {
          			EditText input = (EditText)findViewById(R.id.editText1);
          			int getCursor = input.getSelectionStart();
          			StringBuilder instring = new StringBuilder(input.getText().toString());
					instring.insert(getCursor, (char)(primaryCode));
					input.setText(instring);
					input.setSelection(getCursor+1);
          		 }
          	  }
        	
        });
        keyboardView.setOnKeyListener((new OnKeyListener()
		{
			
			public boolean onKey(View v, int keyCode, KeyEvent event)
			{
				Log.d(TAG, "onKey? keyCode=" + keyCode);
				return false;
			}
		}));
        toggleKeyboardVisibility();
        
        Button sine = (Button)findViewById(R.id.button1);
        Button cos = (Button)findViewById(R.id.button2);
        Button tan = (Button)findViewById(R.id.button3);
        Button asine = (Button)findViewById(R.id.button5);
        Button acos = (Button)findViewById(R.id.button6);
        Button atan = (Button)findViewById(R.id.button7);
        Button sinh = (Button)findViewById(R.id.button8);
        Button cosh = (Button)findViewById(R.id.button9);
        Button tanh = (Button)findViewById(R.id.button10);
        Button asinh = (Button)findViewById(R.id.button11);
        Button acosh = (Button)findViewById(R.id.button12);
        Button atanh = (Button)findViewById(R.id.button13);
        Button ln = (Button)findViewById(R.id.button14);
        Button log = (Button)findViewById(R.id.button15);
        Button ex = (Button)findViewById(R.id.button16);
        Button mod = (Button)findViewById(R.id.button17);
        Button sqrt = (Button)findViewById(R.id.button18);
        Button deriv = (Button)findViewById(R.id.button19);
        
        sine.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "sin(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        asine.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "asin(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        sinh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "sinh(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        asinh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "asinh(  )");
				input.setText(instring);
				input.setSelection(getCursor+7);
			}
        });
        cos.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "cos(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        acos.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "acos(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        cosh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "cosh(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        acosh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "acosh(  )");
				input.setText(instring);
				input.setSelection(getCursor+7);
			}
        });
        tan.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "tan(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        atan.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "atan(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        tanh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "tanh(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        atanh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "atanh(  )");
				input.setText(instring);
				input.setSelection(getCursor+7);
			}
        });
        ln.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "ln(  )");
				input.setText(instring);
				input.setSelection(getCursor+4);
			}
        });
        sqrt.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "sqrt(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        log.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "log(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        ex.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "e^(  )");
				input.setText(instring);
				input.setSelection(getCursor+4);
			}
        });
        mod.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, " % ");
				input.setText(instring);
				input.setSelection(getCursor+3);
			}
        });
        deriv.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "diff(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });

        /*Button parse = (Button)findViewById(R.id.solve);
        parse.setOnClickListener(new Button.OnClickListener() {  
        public void onClick(View v)
            {
        		InputMethodManager inputManager = (InputMethodManager)
        		getSystemService(Context.INPUT_METHOD_SERVICE); 
        		inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

        		EditText input = (EditText)findViewById(R.id.editText1);
        		TextView answerField = (TextView)findViewById(R.id.solution);
                parser = new JEP();
                parser.addStandardConstants();
                parser.addStandardFunctions();
	            parser.parseExpression(input.getText().toString());
                            
                String answer = String.format("%f", parser.getValue());
                answerField.setText(answer);
            }
         });*/
    }
    
    
    protected String findAns(String s)
	{
		for(int i = answers.size(); i>0; i--)
		{
			if(s.contains("ans"+i))
			{
				return findAns(s.replaceAll("ans"+i, answers.get(i-1)));
				
			}
		}
		return s;
	}
    
    protected String formatString(String s)
    {
    	s = s.replaceAll("sqrt", "√");
    	return s;
    }

	public void graph()
	{
		Button sine = (Button)findViewById(R.id.button1);
        Button cos = (Button)findViewById(R.id.button2);
        Button tan = (Button)findViewById(R.id.button3);
        Button asine = (Button)findViewById(R.id.button5);
        Button acos = (Button)findViewById(R.id.button6);
        Button atan = (Button)findViewById(R.id.button7);
        Button sinh = (Button)findViewById(R.id.button8);
        Button cosh = (Button)findViewById(R.id.button9);
        Button tanh = (Button)findViewById(R.id.button10);
        Button asinh = (Button)findViewById(R.id.button11);
        Button acosh = (Button)findViewById(R.id.button12);
        Button atanh = (Button)findViewById(R.id.button13);
        Button ln = (Button)findViewById(R.id.button14);
        Button log = (Button)findViewById(R.id.button15);
        Button ex = (Button)findViewById(R.id.button16);
        Button mod = (Button)findViewById(R.id.button17);
        Button sqrt = (Button)findViewById(R.id.button18);
        
        sine.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "sin(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        asine.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "asin(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        sinh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "sinh(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        asinh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "asinh(  )");
				input.setText(instring);
				input.setSelection(getCursor+7);
			}
        });
        cos.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "cos(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        acos.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "acos(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        cosh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "cosh(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        acosh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "acosh(  )");
				input.setText(instring);
				input.setSelection(getCursor+7);
			}
        });
        tan.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "tan(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        atan.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "atan(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        tanh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "tanh(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        atanh.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "atanh(  )");
				input.setText(instring);
				input.setSelection(getCursor+7);
			}
        });
        ln.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "ln(  )");
				input.setText(instring);
				input.setSelection(getCursor+4);
			}
        });
        sqrt.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "sqrt(  )");
				input.setText(instring);
				input.setSelection(getCursor+6);
			}
        });
        log.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "log(  )");
				input.setText(instring);
				input.setSelection(getCursor+5);
			}
        });
        ex.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, "e^(  )");
				input.setText(instring);
				input.setSelection(getCursor+4);
			}
        });
        mod.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				EditText input = (EditText)findViewById(R.id.editText1);
				int getCursor = input.getSelectionStart();
				StringBuilder instring = new StringBuilder(input.getText().toString());
				instring.insert(getCursor, " % ");
				input.setText(instring);
				input.setSelection(getCursor+3);
			}
        });
        

        Button graph = (Button)findViewById(R.id.graphButton);
        /*
         * This is where the parser will implement the functions input for graphing.
         * 
         * graph.setOnClickListener(new Button.OnClickListener() {  
        public void onClick(View v)
            {
        		EditText input = (EditText)findViewById(R.id.editText1);
        		TextView answerField = (TextView)findViewById(R.id.solution);
                parser = new JEP();
                parser.addStandardConstants();
                parser.addStandardFunctions();
	            parser.parseExpression(input.getText().toString());
                            
                String answer = String.format("%f", parser.getValue());
                answerField.setText(answer);
            }
         });
         
         */
	}

    private void toggleKeyboardVisibility() {
    	  KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboardView);
    	  int visibility = keyboardView.getVisibility();
    	  switch (visibility) {
    	   case View.VISIBLE:
    	    keyboardView.setVisibility(View.INVISIBLE);
    	    break;
    	   case View.GONE:
    	   case View.INVISIBLE:
    	    keyboardView.setVisibility(View.VISIBLE);
    	    break;
    	  }
    	 }

    	 public boolean onKey(View v, int keyCode, KeyEvent event) {
    	  Log.d(TAG, "onKey? keyCode=" + keyCode);
    	  return false;
    	 }

    	 public void swipeUp() {
    	  Log.d(TAG, "swipeUp");
    	 }

    	 public void swipeRight() {
    	  Log.d(TAG, "swipeRight");
    	 }

    	 public void swipeLeft() {
    	  Log.d(TAG, "swipeLeft");
    	 }

    	 public void swipeDown() {
    	  Log.d(TAG, "swipeDown");
    	 }

    	 public void onText(CharSequence text) {
    	  Log.d(TAG, "onText? \"" + text + "\"");
    	 }

    	 public void onRelease(int primaryCode) {
    	  Log.d(TAG, "onRelease? primaryCode=" + primaryCode);
    	 }

    	 public void onPress(int primaryCode) {
    	  Log.d(TAG, "onPress? primaryCode=" + primaryCode);
    	 }

    	 public void onKey(int primaryCode, int[] keyCodes) {
    	  Log.d(TAG, "onKey? primaryCode=" + primaryCode);
    	  int n1 = 0;
    	   Log.v(TAG, "keyCode=" + keyCodes);
    	  }


		public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
		{
			// TODO Auto-generated method stub
			return false;
		}
		
		public void keysize(Keyboard keyboard)
		{
			Display display = getWindowManager().getDefaultDisplay();
			Point size = new Point();
			int width = display.getWidth();
			int height = display.getHeight();
			
		}
}
